package Socket;

import org.json.simple.JSONObject;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;

public class Server {
    static HashSet<Clientpojo>set=new HashSet<Clientpojo>();
    static Integer max=5;
    static String name="小帅啦系统";
    public static void main(String[] args) {
        try {
            System.out.println("服务器运行....");
            ServerSocket serverSocket=new ServerSocket(9999);
            new Thread(new Server_close()).start();
            new Thread(new Server_send()).start();
            while (true)
            {
                Socket socket=serverSocket.accept();
                Clientpojo client=new Clientpojo(socket);
                set.add(client);
                new Thread(new Server_listen(client)).start();
                System.out.println(socket.getInetAddress()+"连接成功.....");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class Server_close implements Runnable{
    @Override
    public void run() {
        while (true){
            try {
                System.out.println(Server.set);
                HashSet<Clientpojo>tem=new HashSet<>(Server.set);
                for(Clientpojo client:Server.set)
                {
                    if(client.close>=5) {
                        Server.set.remove(client);
                        try {
                            client.socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class Server_listen implements Runnable{
    private Clientpojo client;

    public Server_listen(Clientpojo client) {
        this.client = client;
    }
    @Override
    public void run() {
        try {
            ObjectInputStream ois= client.ois;
            while (true) {
                JSONObject jsonObject= (JSONObject) ois.readObject();
                System.out.println(jsonObject);
                if(jsonObject.get("type").equals("heart"))
                    client.name= (String) jsonObject.get("name");
                else {
                    String from= (String) jsonObject.get("from");
                    String to= (String) jsonObject.get("to");
                    String msg=(String)jsonObject.get("msg");
                    JSONObject jsonObject1=new JSONObject();
                    jsonObject1.put("from",from);
                    jsonObject1.put("msg",msg);
                    for(Clientpojo clientpojo:Server.set)
                    {
                        if (clientpojo.name.equals(to))
                        {
                            try {
                                ObjectOutputStream oos= clientpojo.oos;
                                oos.writeObject(jsonObject1);
                                oos.flush();
                                System.out.println(client.socket.getPort());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }
                    }
                }
                //System.out.println(client.socket.getInetAddress()+":"+ois.readObject());
                client.close=0;
            }
        } catch (Exception e) {
            System.out.println(client.socket.getInetAddress()+"断开连接....");
            Server.set.remove(client);
            e.printStackTrace();
        }finally {
            try {
                client.socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
class Server_send implements Runnable{
    @Override
    public void run() {
        while (true)
        {
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入要发送的内容!");
            String str=scanner.next();
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("from",Server.name);
            jsonObject.put("msg",str);
            System.out.println(Server.set);
            for(Clientpojo client:Server.set)
            {
                try {
                ObjectOutputStream oos= client.oos;
                oos.writeObject(jsonObject);
                oos.flush();
                System.out.println(client.socket.getPort());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}